package com.study.dao.impl;

import com.mysql.jdbc.PreparedStatement;
import com.study.dao.CustomerDao;
import com.study.dao.LogDao;
import com.study.model.Customer;
import com.study.model.Log;
import com.study.util.JDBCUtils;
import com.study.util.PageHelper;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.Timestamp;
import java.util.List;

/**
 * @author WS
 */
public class LogDaoImpl implements LogDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public void insertLog(String content, Timestamp time) {
        try {
            String sql = "INSERT INTO log(content,createTime) VALUES(?,?)";
            template.update(sql,content,time);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public PageHelper<Log> queryAllByPagerHelper(int curpage, int pagesize) {
        PageHelper<Log> sel=new PageHelper<>();
        try {
            String sql = "select * from log limit ?,?";
            List<Log> res = template.query(sql,new BeanPropertyRowMapper<Log>(Log.class),(curpage>0?curpage-1:0)*pagesize,pagesize);
            sel.setList(res);
            sql="select count(1) from log";
            sel.setTotal(template.queryForObject(sql,Integer.class));
            sel.setCurPage(curpage);
            sel.setPageSize(pagesize);
            return sel;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
